package vip.mtdp.base.service.rpc.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import vip.mtdp.base.api.app.version.AppVersionRpcService;
import vip.mtdp.base.api.app.version.dto.AppVersionRequest;
import vip.mtdp.base.api.app.version.dto.AppVersionResponse;
import vip.mtdp.base.dal.dao.AppVersionMapper;
import vip.mtdp.base.dal.domain.AppVersion;
import vip.mtdp.common.CommonConstant;
import vip.mtdp.common.exception.AppRTException;

/**
 * @description TODO
 * @date 2020/4/5 17:50
 * @auth wangguoqing
 **/
@Slf4j
@Service
public class AppVersionRpcServiceImpl implements AppVersionRpcService {

    @Autowired
    private AppVersionMapper appVersionMapper;

    @Override
    public boolean canRequest(AppVersionRequest request) throws AppRTException {
        AppVersion appVersion = new AppVersion();
        if(request == null){
            throw new AppRTException("request not null");
        }
        BeanUtils.copyProperties(request,appVersion);

        appVersion.setPreRelease(CommonConstant.BOOLEAN_FALSE);

        AppVersion mAppVersion = appVersionMapper.queryByBuildNo(appVersion);
        appVersion.setForcedUpdate(CommonConstant.BOOLEAN_TRUE);
        AppVersion forcedUpdateVersion = appVersionMapper.queryLastForcedUpdateVersion(appVersion);
        if(mAppVersion == null || forcedUpdateVersion == null){
            throw new AppRTException("根据条件查询数据为空");
        }
        log.info("mAppVersion = {},forcedUpdateVersion = {}",mAppVersion,forcedUpdateVersion);
        //当前版本号需要大于最近的一个强更版本
        if(mAppVersion.getVersion().compareTo(forcedUpdateVersion.getVersion()) >= 0){
            return true;
        }
        return false;

    }

    @Override
    public AppVersionResponse queryLastForcedUpdateVersion(AppVersionRequest request) throws AppRTException {
        log.info("查询最近一个强更版本 request = {}",request);
        AppVersionResponse response = new AppVersionResponse();
        AppVersion appVersion = new AppVersion();
        BeanUtils.copyProperties(request,appVersion);
        appVersion.setPreRelease(CommonConstant.BOOLEAN_FALSE);
        appVersion.setForcedUpdate(CommonConstant.BOOLEAN_TRUE);
        AppVersion mAppVersion = appVersionMapper.queryLastForcedUpdateVersion(appVersion);
        if(mAppVersion != null){
            BeanUtils.copyProperties(mAppVersion,response);
        }
        return response;
    }

    @Override
    public AppVersionResponse queryLastVersion(AppVersionRequest request) throws AppRTException {
        log.info("查询最近一个版本 request = {}",request);
        AppVersionResponse response = new AppVersionResponse();
        AppVersion appVersion = new AppVersion();
        BeanUtils.copyProperties(request,appVersion);
        appVersion.setPreRelease(CommonConstant.BOOLEAN_FALSE);
        AppVersion mAppVersion = appVersionMapper.queryLastVersion(appVersion);
        if(mAppVersion != null){
            BeanUtils.copyProperties(mAppVersion,response);
        }
        return response;
    }

}
